package com.hnust.controller;

import com.hnust.domain.*;
import com.hnust.domain.Class;
import com.hnust.domain.record.*;
import com.hnust.domain.request.ModifyStudentByIdReqBody;
import com.hnust.domain.request.DeleteQuestinRecordReqBody;
import com.hnust.domain.request.GetRecordsListReqBody;
import com.hnust.service.ExerciseService;
import com.hnust.service.StudentService;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@CrossOrigin("*")
@RestController
@RequestMapping
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ExerciseService exerciseService;

    @GetMapping("/getMajor")//浏览器输入：http://localhost:8080/api/student/getMajor?collegeId=123
    public Result<List<MajorRecord>> getMajor(@RequestParam(value="collageID",required = false) String collegeId){
        List<MajorRecord> majorRecord = studentService.getMajor(collegeId);
        if (majorRecord == null){
            return new Result<>(0,"获取专业失败",majorRecord);
        }
        return new Result<>(1,"获取专业成功",majorRecord);
    }

    @GetMapping("/getStudentInfo")//浏览器输入：http://localhost:8080/api/student/getStudentInfo
    public Result<Map<String,Object>> getStudent(
            @RequestParam(value = "studentID",required = false) String studentId,
            @RequestParam(value="token",required = false) String token){
        StudentUpCaseRecord test = studentService.getStudent(studentId,token);
        Map<String,Object> studentRecord = new HashMap<>();
        try {
            studentRecord.put("stuId", test.getStuId());
            studentRecord.put("account", test.getAccount());
            studentRecord.put("alias", test.getAlias());
            studentRecord.put("name", test.getName());
            studentRecord.put("stuNumber", test.getStuNumber());
            studentRecord.put("cls", test.getCls());
            studentRecord.put("grade", test.getGrade());
            studentRecord.put("phoNumber", test.getPhoNumber());
            studentRecord.put("school", test.getSchool());
            studentRecord.put("college", test.getCollege());
            studentRecord.put("major", test.getMajor());
            studentRecord.put("eMail", test.getEMail());
        }catch (Exception e){
            return new Result<>(0, "获取学生信息失败", studentRecord);
        }
        return new Result<>(1,"获取学生信息成功",studentRecord);
    }
    @PostMapping("/changeStudentInfo")//浏览器输入：http://localhost:8080/api/student/changeStudentInfo
    public Result<Map<String,Boolean>> modifyStudentById(@RequestBody String reqsBody){
        String outBody = reqsBody;
        String[] temp1 = reqsBody.split("\\{");
        String inBody = temp1[2].split("}")[0];
        outBody = outBody.replace("\"studentInfo\":{"+inBody+"},","");
        String[] inBodyCouple = inBody.replace("\"","").replace("{","").replace("}","").split(",");
        String[] outBodyCouple = outBody.replace("\"","").replace("{","").replace("}","").split(",");
        ModifyStudentByIdReqBody reqBody = new ModifyStudentByIdReqBody();
        StudentLowerCaseRecord studentInfo = new StudentLowerCaseRecord();
        reqBody.setStudentID(outBodyCouple[1].split(":")[1]);
        reqBody.setToken(outBodyCouple[0].split(":")[1]);
        studentInfo.setStuNumber(inBodyCouple[0].split(":")[1]);
        studentInfo.setCollege(inBodyCouple[1].split(":")[1]);
        studentInfo.setStuId(inBodyCouple[2].split(":")[1]);
        studentInfo.setMajor(inBodyCouple[3].split(":")[1]);
        studentInfo.setSchool(inBodyCouple[4].split(":")[1]);
        studentInfo.setGrade(inBodyCouple[5].split(":")[1]);
        studentInfo.setName(inBodyCouple[6].split(":")[1]);
        studentInfo.setAlias(inBodyCouple[7].split(":")[1]);
        studentInfo.setCls(inBodyCouple[8].split(":")[1]);
        studentInfo.setPhoNumber(inBodyCouple[9].split(":")[1]);
        studentInfo.setAccount(inBodyCouple[10].split(":")[1]);
        studentInfo.setEMail(inBodyCouple[11].split(":")[1]);
        if("0".equals(studentInfo.getCls())) {
            studentInfo.setCls(null);
        }
        reqBody.setStudentInfo(studentInfo);
        //以上为处理
        String msg = studentService.modifyStudentById(reqBody.getStudentID(),reqBody.getStudentInfo(),reqBody.getToken());
        Map<String, Boolean> data = new TreeMap<>();
        int code = 0;
        boolean flag = false;
        if("修改成功".equals(msg)){
            data.put("flag",true);
            code = 1;
        }else {
            data.put("flag",false);
        }
        return new Result<>(code, msg, data);
    }
    @GetMapping("/getCollage")
    public Result<List<College>> getCollege(){
        List<College> colleges = studentService.getCollege();
        if (colleges == null){
            return new Result<>(0,"获取学院失败",colleges);
        }
        return new Result<>(1,"获取学院成功",colleges);
    }

    @GetMapping("/getClass")
    public Result<List<Class>> getClassList(@RequestParam("majorID") String majorId){
        List<Class> classes = studentService.getClassList(majorId);
        if(classes == null){
            return new Result<>(0,"获取班级失败",classes);
        }
        return new Result<>(1,"获取班级成功",classes);
    }
    @PostMapping("/deleteRecord")
    public Result<Map<String,Boolean>> deleteQuestionRecord(@RequestBody DeleteQuestinRecordReqBody deleteQuestinRecordReqBody){
        String msg = studentService.deleteQuestionRecord(deleteQuestinRecordReqBody.getToken(),deleteQuestinRecordReqBody.getRecordIDList());
        Map<String, Boolean> data = new TreeMap<>();
        int code = 0;
        boolean flag = false;
        if("删除成功".equals(msg)){
            data.put("flag",true);
            code = 1;
        }else {
            data.put("flag",false);
        }
        return new Result<>(code, msg, data);
    }
    @GetMapping("/getQuestion")
    public Result<NewQuestionRecordRecord> getQuestion(@RequestParam(value="questionID",required = false)String questionID){
        NewQuestionRecordRecord question = studentService.getQuestion(questionID);
        return new Result<>(1,"获取题目详情成功",question);
    }

    @PostMapping("/getRecordsList")
    public Result<Map<String,Object>> getRecordsList(@RequestBody GetRecordsListReqBody getRecordsListReqBody){

        Map<String,Object> recordsList = studentService.getRecordsList(getRecordsListReqBody.getStudentID(),
                                                                        getRecordsListReqBody.getRecordType(),
                                                                        getRecordsListReqBody.getOffset(),
                                                                        getRecordsListReqBody.getLimit(),
                                                                        getRecordsListReqBody.getToken());

        if (recordsList!=null){
            recordsList.get("questionList");
            return new Result<>(1,"获取成功",recordsList);
        }else {
            return new Result<>(0,"获取失败",recordsList);
        }
    }

    @GetMapping("/getRanking")
    public Result<Map<String,Object>> getRank(@RequestParam(value="colId",required = false) String colId,
                                              @RequestParam(value="majorId",required = false) String majorId,
                                              @RequestParam(value="classId",required = false) String classId,
                                              @RequestParam(value="type",required = false) Integer type,
                                              @RequestParam(value="studentID",required = false) String studentID,
                                              @RequestParam(value="offset",required = false) Integer offset,
                                              @RequestParam(value="limit",required = false) Integer limit){
        Map<String, Object> data = null;
        int code = 0;
        try {
            data = exerciseService.getRanking(colId, majorId, classId, type, studentID, offset, limit);
        }catch (Exception e){
            data = new HashMap<>();
            data.put("msg","参数异常");
        }
        if(data.get("msg").toString().contains("成功")){
            code = 1;
        }
        return new Result(code, (String) data.get("msg"),data);
    }
}
